/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.aut.bme.hu.orkomet.service;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.net.httpserver.*;
import javax.xml.ws.Endpoint;

import edu.aut.bme.hu.orkomet.Application;
import edu.aut.bme.hu.orkomet.Messages;

/**
 *
 * @author Schimi
 */
public class WsPublisher /*extends Thread*/ {
	private Endpoint endpoint;
	private HttpServer server;
	
	class AdobeCrossdomainFileHandler implements HttpHandler
	{
		public static final String ADOBE_CROSSDOMAIN_FILE="<?xml version=\"1.0\" encoding=\"UTF-8\"?> <cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFile.xsd\">   <allow-access-from domain=\"*\" />   <allow-http-request-headers-from domain=\"*\" headers=\"*\" secure=\"true\"/> </cross-domain-policy>";
		public static final String MS_CROSSDOMAIN_FILE="<?xml version=\"1.0\" encoding=\"utf-8\"?><access-policy><cross-domain-access><policy><allow-from http-request-headers=\"*\"> <domain uri=\"*\" /></allow-from><grant-to><resource path=\"/\" include-subpaths=\"true\"/></grant-to></policy></cross-domain-access></access-policy>";
		String responseValue;
		
		@Override
		public void handle(HttpExchange xchg) throws IOException {
			
			 xchg.sendResponseHeaders(200, responseValue.length());
			 OutputStream os = xchg.getResponseBody();
			 DataOutputStream dataoutput= new DataOutputStream(os);
			 dataoutput.writeBytes(responseValue);
			 dataoutput.close();
			 os.close();
		}
		
		AdobeCrossdomainFileHandler(String response)
		{
			responseValue=response;
		}
	
	}
	
	
	//@Override
	public void run() {
		try {
			Logger.getLogger(WsPublisher.class.getName()).log(Level.INFO, Messages.WsPublisher_Publishing);
			int port=Application.getSettingsData().getCommon().getPortNumber();
			String path="/Orkomet";
			URL url=new URL("http","localhost",port,path);
			server=HttpServer.create(new InetSocketAddress(port), 5);
			server.start();
			HttpContext context=server.createContext(path);
			server.createContext("/crossdomain.xml", new AdobeCrossdomainFileHandler(AdobeCrossdomainFileHandler.ADOBE_CROSSDOMAIN_FILE));
			server.createContext("/clientaccesspolicy.xml", new AdobeCrossdomainFileHandler(AdobeCrossdomainFileHandler.MS_CROSSDOMAIN_FILE));
			endpoint=Endpoint.create(new Frontend());
			endpoint.publish(context);
			Logger.getLogger(WsPublisher.class.getName()).log(Level.INFO, String.format(Messages.WsPublisher_Published,url.toString()));
		} catch (Exception e) {
			Logger.getLogger(WsPublisher.class.getName()).log(Level.SEVERE, null, e);		
		}finally{
			Application.getCurrentInstance().notifyObservers();
		}
	}

	public boolean isPublished()
	{
		if(endpoint == null) return false;
		else return endpoint.isPublished();
	}

	public void endPublish()
	{
		try {
			Logger.getLogger(WsPublisher.class.getName()).log(Level.INFO, Messages.WsPublisher_Stopping);
			if(endpoint!=null)
			{
				endpoint.stop();
				server.stop(0);
			}
			Logger.getLogger(WsPublisher.class.getName()).log(Level.INFO, Messages.WsPublisher_Stopped);
		} catch (Exception e) {
			Logger.getLogger(WsPublisher.class.getName()).log(Level.SEVERE, null,e);
		}finally{
			Application.getCurrentInstance().notifyObservers();
		}
	}

}
